﻿
goog.provide('gaillard.RestaurantCommentFragment');

goog.require('goog.dom');
goog.require('goog.ui.Component');
goog.require('goog.ui.Control');
goog.require('gaillard.utils');
goog.require('gaillard.Model');


/**
 * RestaurantCommentFragment
 * @constructor
 * @extends {goog.ui.Component}
 */
gaillard.RestaurantCommentFragment = function(model) {
    goog.ui.Component.call(this);
  
    this.setModel(model);
};

goog.inherits(gaillard.RestaurantCommentFragment, goog.ui.Component);

gaillard.RestaurantCommentFragment.prototype.createDom = function() {
	throw Error('Impossible to create RestaurantCommentFragment from code');
};

gaillard.RestaurantCommentFragment.prototype.enterDocument = function() {
	gaillard.RestaurantCommentFragment.superClass_.enterDocument.call(this);
	
    this.getHandler().listen(this.getModel(), gaillard.Model.Datatype.SELECTED_RESTAURANT, this.selectedRestaurantChanged_);
};


gaillard.RestaurantCommentFragment.prototype.clearCommentList = function() {
    this.removeChildren(true);
};

gaillard.RestaurantCommentFragment.prototype.addComment = function(position, restaurant) {	
	var item = new gaillard.RestaurantCommentItem(this.model_, position, restaurant);
    this.addChild(item, true);
};

gaillard.RestaurantCommentFragment.prototype.selectedRestaurantChanged_ = function () {
    var model = this.getModel(),
        position = model.get(gaillard.Model.Datatype.SELECTED_RESTAURANT),
        restaurantList = model.get(gaillard.Model.Datatype.RESTAURANT_LIST),
        restaurant = restaurantList[position];
    
    
};

/**
 * RestaurantCommentItem
 * @constructor
 * @extends {goog.ui.Control}
 */
gaillard.RestaurantCommentItem = function (model, position, restaurant) {
	goog.ui.Control.call(this);
   
	this.restaurant_ = restaurant;
	this.position_ = position;
	this.setModel(model);
    this.setHandleMouseEvents(true);
	
};

goog.inherits(gaillard.RestaurantCommentItem, goog.ui.Control);

gaillard.RestaurantCommentItem.templateItem = '<a class="grid-item a-nodecodation inline-block" href="#"><div class="grid-item-content"><h1 class="grid-item-name">{{name}}</h2><p>{{description}}</p></div><div class="grid-item-extra"><div class="grid-item-extra-content">Pouet</div></div></a>';

gaillard.RestaurantCommentItem.prototype.createDom = function() {
	var itemStr = gaillard.utils.renderTemplate(gaillard.RestaurantItem.templateItem, this.restaurant_);
	return this.decorateInternal(goog.dom.htmlToDocumentFragment(itemStr));
};

gaillard.RestaurantCommentItem.prototype.decorateInternal = function(element) {
	gaillard.RestaurantCommentItem.superClass_.decorateInternal.call(this, element);
	
    var eh = this.getHandler();
    eh.listen(element, goog.events.EventType.CLICK, this.click_);
    eh.listen(this, goog.ui.Component.EventType.ENTER, this.mouseEnter_);
    eh.listen(this, goog.ui.Component.EventType.LEAVE, this.mouseLeave_);
};





